package com.yiwei.hdb.api.requestInfo;

import android.text.TextUtils;

import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.api.bean.AlipayRechargeV3Bean;
import com.yiwei.hdb.api.bean.LoginBean;
import com.yiwei.hdb.api.bean.PayOrderBean;
import com.yiwei.hdb.api.bean.RepayStatusDialogBean;
import com.yiwei.hdb.api.bean.RepayingDealBean;
import com.yiwei.hdb.api.callback.LoginContract;
import com.yiwei.hdb.api.callback.RepayInfoContract;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;

/**
 * Created by Administrator on 2017/5/14.
 */

public class RepayInfoRequest extends BaseRequest<RepayInfoContract.View> implements RepayInfoContract.Presenter<RepayInfoContract.View> {

    public static RepayInfoRequest create() {
        return new RepayInfoRequest();
    }

    public String SUCCESS = "success";
    public String FAILED = "failed";


    @Override
    public void getRepayInfo(String bank_id) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        RepayingDealBean bean = new RepayingDealBean();
        if (TextUtils.isEmpty(bank_id)) {
            bean.bank_id = "";
        }else {
            bean.bank_id = bank_id;
        }

        Api.api_service.getRepayInfo(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getRepayInfoSuccess(result);
                });
    }

    @Override
    public void getRechargeTips(String money, String account) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();

        AlipayRechargeV3Bean bean = new AlipayRechargeV3Bean();
        bean.money = money;
        bean.payment_account = account;

        Api.api_service.getRechargeTips(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getRechargeTipsSuccess(result);
                });
    }

    @Override
    public void getPayOrder(String action, String payment_id, String channel, String money, String payment_account) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        PayOrderBean bean = new PayOrderBean();
        bean.action = action;
        bean.payment_id = payment_id;
        bean.channel = channel;
        bean.money = money;

        bean.payment_account = payment_account;

        Api.api_service.getPayOrder(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getPayOrderSuccess(result);
                });
    }

    @Override
    public void getRepayDialog() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();

        Api.api_service.getRepayDialog().compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getRepayDialogSuccess(result);
                });
    }

    @Override
    public void confirmDialog(String notice_id, String confirm) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        RepayStatusDialogBean bean = new RepayStatusDialogBean();
        bean.notice_id = notice_id;
        bean.confirm = confirm;
        Api.api_service.getConfirmDilaog(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.confirmDialogSuccess(result, confirm);
                });
    }

    @Override
    public void getPayWay() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
//        mView.onRequestStart();
        Api.api_service.getPayWay().compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getPayWaySuccess(result);
                });
    }
}
